home *** CD-ROM | disk | FTP | other *** search
/ .net 2002 March / DotNetMagazine-Issue107-Coverdisc-NET107-02-03-PCMac.bin / pc / PC Software / free_browsing / DavesQckSearchDbar3-14 / dqsd.exe / translate.js < prev    next >
Text File  |  2002-01-04  |  4KB  |  152 lines

  1. // code to do translations
  2.  
  3. function translate(t)
  4. {
  5.   // detect strings that ask for a translation
  6.   var trans = "(en_no|en_zh|en_fr|en_de|en_it|en_ja|en_ko|en_pt|en_es|zh_en|fr_en|fr_de|de_en|de_fr|it_en|ja_en|ko_en|pt_en|ru_en|es_en|" +
  7.                "en-no|en-zh|en-fr|en-de|en-it|en-ja|en-ko|en-pt|en-es|zh-en|fr-en|fr-de|de-en|de-fr|it-en|ja-en|ko-en|pt-en|ru-en|es-en)";
  8.   var prot = new RegExp("^" + trans + "(.+)","gi");
  9.   var prot2 = new RegExp("(.+)" + trans + "$","gi");
  10.   if (prot.exec(t))
  11.   {
  12.      var temp1 = t.replace(prot,"$1").replace("-", "_");
  13.      var temp2 = t.replace(prot,"$2");
  14.      choosetranslate(temp1,temp2);
  15.      return true;
  16.   }
  17.   else if (prot2.exec(t))
  18.   {
  19.      var temp2 = t.replace(prot2,"$1");
  20.      var temp1 = t.replace(prot2,"$2").replace("-", "_");
  21.      choosetranslate(temp1,temp2);
  22.      return true;
  23.   }
  24.   else
  25.      return false;
  26. }
  27.  
  28. var FREE_TRANSLATIONS = 
  29. {
  30.   es_en: "Spanish/English",
  31.   fr_en: "French/English",
  32.   de_en: "German/English",
  33.   it_en: "Italian/English",
  34.   pt_en: "Portugese/English",
  35.   en_es: "English/Spanish",
  36.   en_fr: "English/French",
  37.   en_it: "English/Italian",
  38.   en_no: "English/Norwegian",
  39.   en_pt: "English/Portungese"
  40. };
  41.  
  42. function choosetranslate(l,q)
  43. {
  44.   if (FREE_TRANSLATIONS[l] != null)
  45.     freetranslate(FREE_TRANSLATIONS[l], q);
  46.   else
  47.     babeltranslate(l, q);
  48. }
  49.  
  50. function babeltranslate(l,q)
  51. {
  52.   //q is the query, l is the language choices
  53.   var prot = new RegExp("^(http://|https://)([\\-a-z0-9]+\\.)*[\\-a-z0-9]+" +
  54.                        "|^[a-z]:($|\\\\)" +
  55.                        "|^\\\\\\\\[a-z]+($|\\\\($|[a-z]+($|\\\\)))", "i");
  56.   var dns = new RegExp("^www\.([\\-a-z0-9]+\\.)+[\\-a-z0-9]+$" +
  57.                    "|^([\\-a-z0-9]+\\.)+(com|net|org|edu|gov|mil|[a-z]{2})$", "i");
  58.  
  59.   q = q.replace(/^\s*/, '').replace(/\s*$/, '');
  60.  
  61.   babl.lp.value = l;
  62.  
  63.   if (dns.exec(q))
  64.   {
  65.     babl.tt.value = "url";
  66.     babl.url.value = "http://" + q;
  67.   }
  68.   else if (prot.exec(q))
  69.   {
  70.     babl.tt.value = "url";
  71.     babl.url.value = q;
  72.   }
  73.   else
  74.   {
  75.     babl.tt.value = "urltext";
  76.     babl.urltext.value = q;
  77.   }
  78.   submitForm(babl);
  79. }
  80.  
  81. function freetranslate(l,q)
  82. {
  83.   //q is the query, l is the language choices
  84.   var prot = new RegExp("^(http://|https://)([\\-a-z0-9]+\\.)*[\\-a-z0-9]+" +
  85.                        "|^[a-z]:($|\\\\)" +
  86.                        "|^\\\\\\\\[a-z]+($|\\\\($|[a-z]+($|\\\\)))", "i");
  87.   var dns = new RegExp("^www\.([\\-a-z0-9]+\\.)+[\\-a-z0-9]+$" +
  88.                    "|^([\\-a-z0-9]+\\.)+(com|net|org|edu|gov|mil|[a-z]{2})$", "i");
  89.  
  90.   q = q.replace(/^\s*/, '').replace(/\s*$/, '');
  91.   if (dns.exec(q))
  92.   {
  93.     fturl.Language.value = l;
  94.     fturl.Url.value = "http://" + q;
  95.     submitForm(fturl);
  96.   }
  97.   else if (prot.exec(q))
  98.   {
  99.     fturl.Language.value = l;
  100.     fturl.Url.value = q;
  101.     submitForm(fturl);
  102.   }
  103.   else
  104.   {
  105.     fttext.Language.value = l;
  106.     fttext.SrcText.value = q;
  107.     submitForm(fttext);
  108.   }
  109. }
  110.  
  111. // form for Babelfish translation
  112.  
  113. document.write(
  114.   "<form name=babl"
  115. + "      target=_blank"
  116. + "      action=http://babelfish.altavista.com/tr"
  117. + "      method=post>"
  118. + "  <input type=hidden name=doit value=done>"
  119. + "  <input type=hidden name=lp>"
  120. + "  <input type=hidden name=tt>"
  121. + "  <input type=hidden name=urltext>"
  122. + "  <input type=hidden name=url>"
  123. + "</form>");
  124.  
  125. // form for FreeTranlsation.com translation by url
  126.  
  127. document.write(
  128.   "<form name=fturl"
  129. + "      target=_blank"
  130. + "      action=http://fets3.freetranslation.com:5081/"
  131. + "      method=get>"
  132. + "  <input type=hidden name=Language>"
  133. + "  <input type=hidden name=Url>"
  134. + "  <input type=hidden name=Sequence VALUE=core>"
  135. + "</form>");
  136.  
  137.  
  138. // form for FreeTranlsation.com translation of text
  139. document.write(
  140.   "<form name=fttext"
  141. + "      target=_blank"
  142. + "      action=http://ets.freetranslation.com:5081/"
  143. + "      method=get>"
  144. + "  <input type=hidden name=Sequence VALUE=core>"
  145. + "  <input type=hidden name=Mode value=html>"
  146. + "  <input type=hidden name=template value=TextResults2.htm>"
  147. + "  <input type=hidden name=Language>"
  148. + "  <input type=hidden name=SrcText>"
  149. + "</form>");
  150.  
  151.  
  152.